package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.IAdvertiseDao;
import com.magicode.cms.entiy.base.Advertise;
import com.magicode.cms.entiy.base.Dictionary;
import com.magicode.cms.service.IAdvertiseService;
import com.magicode.common.dao.ICommonDaoService;

@Service("serviceadvertise")
public class AdvertiseServiceImpl extends
		BaseServiceImpl<Advertise, IAdvertiseDao> implements IAdvertiseService {

	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql = "from " + Advertise.class.getName() + " "
				+ entity_alias_name + " ";
		hql += " left join "+entity_alias_name+".adPosition ";
		hql += " left join "+entity_alias_name+".mainPic ";
		hql += " left join "+entity_alias_name+".type ";
		hql += " left join "+entity_alias_name+".author ";
		hql += " left join "+entity_alias_name+".updateOperate ";
		hql += " left join "+entity_alias_name+".videoList ";
		
		if (null != map && !map.isEmpty()) {
			hql += "where 1 = 1 ";
			if (map.containsKey(field_id)) {
				hql = hql + " and " + entity_alias_name + ".id = '"
						+ map.get(field_id) + "'";
			}
			if (map.containsKey("title")) {
				hql = hql + " and " + entity_alias_name + ".title like '%"
						+ map.get("title") + "%'";
			}
//			if (null!=map && map.containsKey("describeBeginTime")) {
//				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".describeBeginTime,'"+sql_date_format+"')  '"+map.get("describeBeginTime")+"'";
//			}	
//			if (map.containsKey("describeEndTime")) {
//				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".describeEndTime,'"+sql_date_format+"')  '"+map.get("describeEndTime")+"'";
//			}	
			if (map.containsKey("beginDateFrom")){
				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".describeBeginTime,'"+sql_date_format+"') >= '"+map.get("beginDateFrom")+"'";
			}
			if (map.containsKey("beginDateTo")){
				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".describeBeginTime,'"+sql_date_format+"') <= '"+map.get("beginDateTo")+"'";
			}
			if (map.containsKey("endDateFrom")){
				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".describeEndTime,'"+sql_date_format+"') >= '"+map.get("endDateFrom")+"'";
			}
			if (map.containsKey("endDateTo")){
				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".describeEndTime,'"+sql_date_format+"') <= '"+map.get("endDateTo")+"'";
			}
		}
		return hql;
	}


	/**
	 * 广告位列表
	 */
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "广告名称");
		map.put("width", 140);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "adPosition");
		map.put(json_title, "广告位");
		map.put("width", 130);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "status");
		map.put(json_title, "状态");
		map.put("width", 40);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "type");
		map.put(json_title, "类型");
		map.put("width", 35);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "href");
		map.put(json_title, "链接地址");
		map.put("width", 120);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "describeBeginTime");
		map.put(json_title, "发布时间");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "describeEndTime");
		map.put(json_title, "结束时间");
		map.put("width", 90);
		beaninfo.add(map);
		
		return beaninfo;
	}

	@Override
	public List<JsonOperation> getOpts(String url, String id) {
		List<JsonOperation> opts = new ArrayList<JsonOperation>();
		JsonOperation opt = new JsonOperation();
		opt.setName("修改");
		opt.setUrl(getCtx().getContextPath() + "/" + url + "/getinfo/" + id
				+ "/edit" + url);
		opt.setType("edit");
		opts.add(opt);
		opt = new JsonOperation();
		opt.setName("删除");
		opt.setUrl(getCtx().getContextPath() + "/" + url + "/" + id
				+ "?_requestMethod=delete");
		opt.setType("delete2");
		opts.add(opt);
//		opt = new JsonOperation();
//		opt.setName("预览");
//		opt.setUrl(getCtx().getContextPath() + "/p/advertise/sec/" + id + "");
//		opt.setType("preview");
//		opts.add(opt);
		return opts;
	}

	@Override
	public IAdvertiseDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return advertiseDao;
	}

	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
	
	@Autowired
	@Qualifier("daoadvertise")
	private IAdvertiseDao advertiseDao;

	public IAdvertiseDao getAdvertiseDao() {
		return advertiseDao;
	}

	public void AdvertiseDao(IAdvertiseDao advertiseDao) {
		this.advertiseDao = advertiseDao;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0,
					field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20,
					field_grid_size);
			String hql = this.getHql(querywhere);
//			 Map<String,String> gridsel = new HashMap<String,String>();

			hql += " order by t.createTime desc ";
			Map<String, String> gridsel = new HashMap<String, String>();
			gridsel.put(field_id, "" + entity_alias_name + ".id as id");
			gridsel.put(field_title, "" + entity_alias_name + ".title as title");
			gridsel.put("type", "" + entity_alias_name + ".type.title as type");
			gridsel.put("describeBeginTime", sql_date_method + "("
					+ entity_alias_name + ".describeBeginTime ,'"
					+ sql_time_format + "') as describeBeginTime ");
			gridsel.put("describeEndTime", sql_date_method + "("
					+ entity_alias_name + ".describeEndTime ,'"
					+ sql_time_format + "') as describeEndTime");
			gridsel.put("href", "(" + entity_alias_name + ".href ) as href");
			gridsel.put("adPosition", "" + entity_alias_name + ".adPosition.title as adPosition");
			gridsel.put("status", "case when "+entity_alias_name+".status = 0 then '草稿' "
					+ " when "+entity_alias_name+".status = 1 then '待审核' "
					+ " when "+entity_alias_name+".status = 2 then '已发布'  end as status ");
			return this.queryMapDataList(hql, gridsel, start, size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}

	}


	@Override
	public Advertise getAdvertiseByCode(String code) {
		String sql = "select a from "+Advertise.class.getName()
				+" a left join a.adPosition left outer join fetch a.mainPic where a.adPosition.code = '"+code+" '";
				
		Advertise advertise = null;
		List<Advertise> list = new ArrayList<Advertise>();
		try {
			list = this.commonDaoService.queryList(sql, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (Advertise advertise2 : list) {
			Long begin_time = advertise2.getDescribeBeginTime().getTime();
			Long end_time = advertise2.getDescribeEndTime().getTime();
			Long now_time = new Date().getTime();
			if(begin_time<now_time&&now_time<end_time){
				advertise = advertise2;
			}
		}
		return advertise;
	}


	@Override
	public List<Advertise> getAdvertiseListByCode(String code) {
		// TODO Auto-generated method stub
		return null;
	}
}
